package com.eco.fishway.netty;

import com.eco.fishway.util.HexStringUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;



@Slf4j
public class KndEncoder extends MessageToByteEncoder<String> {


    @Override
    public void encode(ChannelHandlerContext channelHandlerContext, String msg, ByteBuf out) throws Exception {
        try {
            byte[] bytes = hexString2Bytes(msg);
            out.writeBytes(bytes);
        } catch (Exception e) {
            log.error("knd-tcp自定义编码器异常:{}", e.toString());
        }
    }

    private byte[] hexString2Bytes(String src) throws Exception {
        return HexStringUtil.hexString2Bytes(src);
    }
}
